package me.passtime.projt;

import android.util.Log;
import me.passtime.projt.Const;
import me.passtime.projt.Home;
import me.passtime.projt.Main;
import me.passtime.projt.db.Account;
import me.passtime.projt.db.DB;
import me.passtime.projt.http.AbstractHttpController;
import me.passtime.projt.http.UriConstants;
import me.passtime.projt.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-12
 * Time: 下午5:53
 */
public class MainController extends AbstractHttpController<String> {
    private Main mActivity;
    private DB mDB;
    private String phoneNumber;
    private String password;
    private String userName="张三";

    /**
     * 初始化网络连接工具类和GUI Handler。
     */
    public MainController(Main main) {
        super(String.class);
        this.mActivity = main;
        mDB = DB.getInstance(mActivity);
    }

    @Override
    public void onNetworkAbort() {
        ToastUtils.showMessage(mActivity, "错误，网络连接被中断.");
        mActivity.hideProgressDialog();
    }

    @Override
    public void onNetworkError() {
        ToastUtils.showMessage(mActivity, "网络连接错误");
        mActivity.hideProgressDialog();
    }

    @Override
    public void onRequestFinish(String response) {
        mActivity.hideProgressDialog();
        if (ResponseCode.SUCCESS.equals(response)) {
            UserHolder.phoneNumber = phoneNumber;
            UserHolder.password = password;
            UserHolder.userName = userName;
            if (mActivity.rememberMeChecked) {
                rememberUserInfo(phoneNumber, password);
            }
            PageJumpUtils.jump(mActivity, Home.class);
        }else{
            ToastUtils.showMessage(mActivity, "错误，登陆失败");
        }
        if (mActivity.rememberMeChecked) {
            rememberUserInfo(phoneNumber, password);
        }
        PageJumpUtils.jump(mActivity, Home.class);
        ToastUtils.showMessage(mActivity, response);
    }

    @Override
    public void onResponseError() {
        ToastUtils.showMessage(mActivity, "错误，返回结果错误");
        mActivity.hideProgressDialog();
    }


    public void login(String phoneNumber, String password) {
        this.phoneNumber = phoneNumber;
        this.password = password;
        String xml = new PostXmlBuilder().setBiPCode(RequestCode.LOGIN_AUTH).setTelNo(phoneNumber).setUserPas(password).createXml();
        this.sendRequest(UriConstants.LOGIN, xml);
    }

    public void rememberUserInfo(String phoneNumber, String password) {
        Log.d(Const.TAG, "do login");
        if (mActivity.rememberMeChecked) {
            mDB.open();
            Account account = mDB.queryAccountByPhoneNumber(phoneNumber);
            if (account == null) {
                account = new Account();
                account.phoneNumber = phoneNumber;
                account.password = password;
                account.remember = "T";
                mDB.insertAccount(account);
            } else {
                mDB.rememberAccount(phoneNumber, password);
            }
            mDB.close();
        } else {
            mDB.open();
            mDB.clearRememberAccount(phoneNumber);
            mDB.close();
        }
    }
}
